Skip to main content

Game Data

Saving and reading data between scenes or play sessions is easily achieved using the GameData class.

Saving Data

You only need one method to save all of your data.

using ExtensionTools.Data;

...

GameData.SetData("position", Vector2.one);
GameData.SetData("name", "test string");
GameData.SetData("color", Color.green);
GameData.SetData("player", new Player());
...

You can use the SetData method with the first variable being a variable name and the second being the value to save any primitive type or even custom structs or classes. However when saving a custom class or struct make sure these are marked serializable!

Reading Data

Similar to saving data, you only need one method to read data.

using ExtensionTools.Data;

...

Vector3 value;
if (GameData.TryGetData("position", out value))
{
//Value is found
}
else
//Value not found
...

Saving And Loading Data to/from Disk

Saving and loading data to/from the disk is required if you want to store save files for usage between sessions.

Thankfully this can easily be done.

using ExtensionTools.Data;

...

//Save the current data to the disk under a default saveFile in the default path
GameData.SaveToDisk();

//Try to load data from the disk under a default saveFile in the default Path
GameData.TryLoadFromDisk();

By default the data will be saved and read from the Application.persistentDataPath with default being the filename. This can however be changed.

GameData.SaveToDisk("profile1","C:/Saves/");
GameData.TryLoadFromDisk("profile1","C:/Saves/");

Deleting data

Sometimes you might want to remove some data. To do this simply call the RemoveData method.

GameData.RemoveData("position");

To delete a whole savefile, you can use the DeleteSaveFileFromDisk method. With the two optional parameters being once again the name of the savefile and the path.

GameData.DeleteSaveFileFromDisk("profile1","C:/Saves/");